<script>
  export let label = "";
  export let checked = false;
  export let readonly = false;
  export let disabled = false;
</script>

<div class="switch-field">
  <label class="switch-label" class:readonly class:disabled>
    {#if readonly}
      <span class="readonly-status" class:enabled={checked}>
        {checked ? "开启" : "关闭"}
      </span>
    {:else}
      <input type="checkbox" bind:checked {disabled} />
      <span class="switch"></span>
    {/if}
    <span class="switch-text">{label}</span>
  </label>
</div>

<style>
  .switch-field {
     display: inline-block
  }

  .switch-field:last-child {
    margin-bottom: 0;
  }

  .switch-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    font-size: 14px;
    color: #121212;
    gap: 8px;
  }

  .switch-label.readonly {
    cursor: default;
  }

  .switch-label.disabled {
    cursor: not-allowed;
    opacity: 0.6;
  }

  .switch-label input {
    display: none;
  }

  .switch-text {
    flex: 1;
  }

  .switch {
    width: 26px;
    height: 14px;
    background: #e0e0e0;
    border-radius: 7px;
    position: relative;
    transition: background 0.3s;
    flex-shrink: 0;
  }

  .switch::after {
    content: "";
    width: 10px;
    height: 10px;
    background: #fff;
    border-radius: 50%;
    position: absolute;
    top: 2px;
    left: 2px;
    transition: transform 0.3s;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  }

  .switch-label input:checked + .switch {
    background: rgba(69, 81, 229, 1);
  }

  .switch-label input:checked + .switch::after {
    transform: translateX(12px);
  }

  .switch-label input:disabled + .switch {
    background: #f5f5f5;
    cursor: not-allowed;
  }

  .switch-label input:disabled:checked + .switch {
    background: #bfbfbf;
  }

  .readonly-status {
    font-size: 13px;
    padding: 2px 8px;
    border-radius: 12px;
    color: #666;
  }

  .readonly-status.enabled {
    background: rgba(69, 81, 229, 0.1);
    color: rgba(69, 81, 229, 1);
  }

  .readonly-status:not(.enabled) {
    background: #f5f5f5;
    color: #999;
  }
</style>
